#include <linux/input.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdint.h>

#define KEY_EVENT_DEV0 "/dev/input/event0"
#define KEY_EVENT_DEV1 "/dev/input/event1"

int sys_key_scan(void)
{
    int ret = -1;
    int i = 0;
    int key_fd[2] = {0};
    struct pollfd key_fds[2] = {0};
    struct input_event key_event = {0};

    key_fd[0] = open(KEY_EVENT_DEV0, O_RDONLY);
    if (key_fd[0] <= 0) {
        printf("opne device 0 error!\n");
        return ret;
    }

    key_fd[1] = open(KEY_EVENT_DEV1, O_RDONLY);
    if (key_fd[1] <= 0) {
        printf("open device 1 error!\n");
        return ret;
    }

    for (i = 0; i < 2; i++) {
        key_fds[i].fd = key_fd[i];
        key_fds[i].events = POLLIN;
    }

    while (1) {

        ret = poll(key_fds, 2, -1);

        for (i = 0; i < 2; i++) {
            ret = lseek(key_fd[i], 0, SEEK_SET);
            ret = read(key_fd[i], &key_event, sizeof(key_event));

            if (ret) {
                if (key_event.type == EV_KEY && (key_event.value == 0 || key_event.value == 1))
                    printf("key value (%d) %s", key_event.code, key_event.value ? "press" : "release");
            }
        }
    }

    close(key_fd[0]);
    close(key_fd[1]);

    return ret;
}

int main(int argc, char * argv[])
{
    printf("This is a key event test.\n");

    sys_key_scan();

    return 0;
}
